﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using AdoNet = System.Data;
using System.Runtime.CompilerServices;

namespace DataSet4Com
{
    [ComVisible(true)]
    [ClassInterface(ClassInterfaceType.AutoDispatch)]
    [ComDefaultInterface(typeof(IDataTableCollection))]
    [Guid("E2FFC61B-0E3A-45D3-9A71-85EDB4E4A702")]
    public class DataTableCollection : IDataTableCollection
    {
        private AdoNet.DataTableCollection _adoNetTableCollection;

        public DataTableCollection() : this(null) { }

        public DataTableCollection(AdoNet.DataTableCollection adoNetTableCollection)
        {
            if (adoNetTableCollection == null)
                throw new ArgumentNullException("adoNetTableCollection");
            else
                _adoNetTableCollection = adoNetTableCollection;
        }

        [ComVisible(false)]
        public AdoNet.DataTableCollection AdoNetDataTableCollection
        {
            get { return _adoNetTableCollection; }
        }

        [DispId(-4)]
        public IEnumerator GetEnumerator()
        {
            var result = from AdoNet.DataTable table in _adoNetTableCollection
                         select new DataTable(table);

            return result.GetEnumerator();
        }

        [DispId(0)]
        [IndexerName("_Default")]
        public DataTable this[int index]
        {
            get
            {
                return new DataTable(_adoNetTableCollection[index]);
            }
        }

        public DataTable Add()
        {
            return new DataTable(_adoNetTableCollection.Add());
        }

        public void AddTable(DataTable table)
        {
            _adoNetTableCollection.Add(table.AdoNetDataTable);
        }

        public DataTable AddWithName(string name, [Optional, DefaultParameterValue("")] string tableNamespace)
        {
            if (string.IsNullOrEmpty(tableNamespace))
                return new DataTable(_adoNetTableCollection.Add(name));
            else
                return new DataTable(_adoNetTableCollection.Add(name, tableNamespace));
        }

        public bool CanRemove(DataTable table)
        {
            return _adoNetTableCollection.CanRemove(table.AdoNetDataTable);
        }

        public void Clear()
        {
            _adoNetTableCollection.Clear();
        }

        public bool Contains(string name, [Optional, DefaultParameterValue("")] string tableNamespace)
        {
            if (string.IsNullOrEmpty(tableNamespace))
                return _adoNetTableCollection.Contains(name);
            else
                return _adoNetTableCollection.Contains(name, tableNamespace);
        }

        public int IndexOf(DataTable table)
        {
            return _adoNetTableCollection.IndexOf(table.AdoNetDataTable);
        }

        public int IndexOfByTableName(string name, [Optional, DefaultParameterValue("")] string tableNamespace)
        {
            if (string.IsNullOrEmpty(tableNamespace))
                return _adoNetTableCollection.IndexOf(name);
            else
                return _adoNetTableCollection.IndexOf(name, tableNamespace);
        }

        public void Remove(DataTable table)
        {
            _adoNetTableCollection.Remove(table.AdoNetDataTable);
        }

        public void RemoveByTableName(string name, [Optional, DefaultParameterValue("")] string tableNamespace)
        {
            if (string.IsNullOrEmpty(tableNamespace))
                _adoNetTableCollection.Remove(name);
            else
                _adoNetTableCollection.Remove(name, tableNamespace);
        }

        public void RemoveAt(int index)
        {
            _adoNetTableCollection.RemoveAt(index);
        }

        public int Count
        {
            get { return _adoNetTableCollection.Count; }
        }
    }

    [ComVisible(true)]
    [InterfaceType(ComInterfaceType.InterfaceIsDual)]
    [Guid("438B2E10-69B3-42A7-9154-0CA9CB17F649")]
    public interface IDataTableCollection
    {
        [DispId(-4)]
        IEnumerator GetEnumerator();

        [DispId(0)]
        [IndexerName("_Default")]
        DataTable this[int index]
        {
            get;
        }

        DataTable Add();

        void AddTable(DataTable table);

        DataTable AddWithName(string name, [Optional, DefaultParameterValue("")] string tableNamespace);

        bool CanRemove(DataTable table);

        void Clear();

        bool Contains(string name, [Optional, DefaultParameterValue("")] string tableNamespace);

        int IndexOf(DataTable table);

        int IndexOfByTableName(string name, [Optional, DefaultParameterValue("")] string tableNamespace);

        void Remove(DataTable table);

        void RemoveByTableName(string name, [Optional, DefaultParameterValue("")] string tableNamespace);

        void RemoveAt(int index);

        int Count
        {
            get;
        }
    }
}
